package com.eudon.ai.agent.etl.reader;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author eudon
 * @description md文档读取 ETL extract部分
 * @date 2025/8/11
 * @email syd19970616@gmail.com
 * @address <a href="https://gitee.com/eudon">Gitee</a>
 */
@Component
@Slf4j
public class MdDocsReader {

    private final ResourcePatternResolver resolver;

    public MdDocsReader(ResourcePatternResolver resolver) {
        this.resolver = resolver;
    }

    public List<Document> loadMdDocs() throws IOException {
        Resource[] resources = resolver.getResources("classpath:static/docs/*.md");
        List<Document> documents = new ArrayList<>();
        for (Resource resource : resources) {
            String filename = resource.getFilename();
            String postfix = filename.substring(filename.length() - 6, filename.length() - 4);
            MarkdownDocumentReaderConfig config = MarkdownDocumentReaderConfig.builder()
                    .withHorizontalRuleCreateDocument(true)
                    .withIncludeCodeBlock(false)
                    .withIncludeBlockquote(false)
                    .withAdditionalMetadata("author", "eudon")
                    .withAdditionalMetadata("status", postfix)
                    .build();
            MyMarkdownDocumentReader reader = new MyMarkdownDocumentReader(resource, config);
            documents.addAll(reader.get());
        }
        return documents;
    }
}
